package main

import (
	"flag"
	"k3s-api-test/pkg/client"
	"k3s-api-test/pkg/tests/deployment"
	"k3s-api-test/pkg/tests/pod"
	"k3s-api-test/pkg/tests/service"
	"k3s-api-test/pkg/utils"
	"k8s.io/client-go/kubernetes"
	"strings"
	"time"
)

const (
	testPodName      = "pod"
	testDeployName   = "deploy"
	testServiceName  = "service"
	testDefaultValue = "all"
)

func main() {
	// 默认使用Master 的 kubeconfig
	kubeconfig := flag.String("kubeconfig", "/etc/rancher/k3s/k3s.yaml", "Path to kubeconfig file")

	var chooseArgs string
	flag.StringVar(&chooseArgs, "test", "all", "")
	// 匹配命令行参数，默认值(这里我们输入具体的操作内容），描述信息。
	testPodArgs := flag.String(testPodName, testDefaultValue, "Specify with test pod case to run")
	testDeployArgs := flag.String(testDeployName, testDefaultValue, "Specify with test deploy case to run")
	testServiceArgs := flag.String(testServiceName, testDefaultValue, "Specify with test service case to run")
	flag.Parse() // 解析命令行参数

	log := utils.SetupLogger()
	// 初始化k3s client
	var clientset *kubernetes.Clientset
	var err error
	for i := 0; i < 3; i++ {
		clientset, err = client.NewClient(*kubeconfig)
		if err == nil {
			break
		}
		log.Warnf("Failed to create k3s client (attempt %d): %v", i+1, err)
		time.Sleep(2 * time.Second)
	}
	if err != nil {
		log.Fatalf("Failed to create k3s client after retries: %v", err)
	}

	// 运行测试
	log.Info("Starting k3s API tests on master node")
	// 根据不同的参数执行不同系列的测试,输入 -test=P -pod=basic/field/container 可以执行三个测试之一
	switch chooseArgs {
	case "P":
		pod.RunPodTests(clientset, log, strings.ToLower(*testPodArgs))
	case "D":
		deployment.RunDeploymentTests(clientset, log, strings.ToLower(*testDeployArgs))
	case "S":
		service.RunServiceTests(clientset, log, strings.ToLower(*testServiceArgs))
	default:
		log.Info("Sunshine with independent mind")
	}
	//pod.RunPodTests(clientset, log, strings.ToLower(*testPodArgs))
	//deployment.RunDeploymentTests(clientset, log, strings.ToLower(*testDeployArgs))
	// TODO
	//node.RunTests(clientset, log)
	//service.RunTests(clientset, log)
	//configmap.RunTests(clientset, log)
	//pvc.RunTests(clientset, log)
	//ingress.RunTests(clientset, log)
	//storageclass.RunTests(clientset, log)
	//job.RunTests(clientset, log)
	//rbac.RunTests(clientset, log)

	log.Info("Your tests completed.")
}
